#include <stdio.h>
#include <libusb-1.0/libusb.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
 //
#define USB_VENDOR_ID   0x10c4
#define USB_PRODUCT_ID  0xea61
typedef unsigned char u8;




 
struct userDevice{
	/*Device descriptor*/
 
	/** USB-IF vendor ID */
	uint16_t idVendor;
 
	/** USB-IF product ID */
	uint16_t idProduct;
 
	/*Interface descriptor*/
 
	/** USB-IF class code for this interface. See \ref libusb_class_code. */
	uint8_t  bInterfaceClass;
 
	/** USB-IF subclass code for this interface, qualified by the
	 * bInterfaceClass value */
	uint8_t  bInterfaceSubClass;
 
	/*Endpoint descriptor*/
 
	
	
	uint8_t  bmAttributes;
 
	
	/*save parameter*/
 
	libusb_device *dev;
	libusb_device **devs;
 
 
	uint8_t bInEndpointAddress;
 
	
 
	/* Number of this interface */
	int  bInterfaceNumber;
};

struct date{
	int year;
	int month;
	int day;
	int min;
	int hour;
	int sec;
};

 
 
int init_libusb(void)
{
	/*1. init libusb lib*/
	int rv = 0;
	
	rv = libusb_init(NULL);
	if(rv < 0) {
		
		perror("\nInit libusb error");  
		return -1;
	}
	return rv;
}
 
 
int get_device_descriptor(struct libusb_device_descriptor *dev_desc,struct userDevice *user_device)
{
	/*2.get device descriptor*/
	int rv = -2;
	ssize_t cnt;
	int i = 0;
 
	libusb_device **devs;
	libusb_device *dev;
 
	cnt = libusb_get_device_list(NULL, &devs); //check the device number
	if (cnt < 0)
		return (int) cnt;
 
	while ((dev = devs[i++]) != NULL) {
		rv = libusb_get_device_descriptor(dev,dev_desc);
		if(rv < 0) {
			
			perror("\nlibusb_get_device_descriptor error");
			return -1;
		}
		if(dev_desc->idProduct==user_device->idProduct &&dev_desc->idVendor==user_device->idVendor)
		{
			user_device->dev = dev;
			user_device->devs = devs;
			rv = 0;
			printf("%04x:%04x\n",dev_desc->idProduct,dev_desc->idVendor);
			break;
		}
	}
	return rv;
}

/*
void getFileName(char *filename,char *localtime){
	time_t tm_ptr;
	struct tm *ptr = NULL;
	time(&tm_ptr);
	ptr = gmtime(&tm_ptr);
	sprintf(filename,"gps_%d_%d_%d_%d_%d_%d.bag",(1900+ptr->tm_year),(1+ptr->tm_mon),ptr->tm_mday,ptr->tm_hour+8,ptr->tm_min,ptr->tm_sec);
	sprintf(localtime,"\n\ntime %d_%d_%d_%d_%d_%d\n",(1900+ptr->tm_year),(1+ptr->tm_mon),ptr->tm_mday,ptr->tm_hour+8,ptr->tm_min,ptr->tm_sec);
}

*/
void substring(char *des,char *str,int n,int index){
	char *p = des;
	char *q = str;
	int len = strlen(str);
	
	if(n>len) n = len-index;
	if(index<0) index=0;
	if(index>len) return ;
	q += index;
	while(n--) *(p++) = *(q++);
	*(p++) = '\0';
	
}
 
int main(int argc,char *argv[])
{
	int rv;
 
	int length;
 
	unsigned char buf[32*15] = {0};
 
	libusb_device_handle* g_usb_handle;
 
	struct userDevice user_device;
	struct libusb_device_descriptor dev_desc;
 
	user_device.idProduct = USB_PRODUCT_ID;
	user_device.idVendor =  USB_VENDOR_ID ;
	user_device.bInterfaceClass = 0xff ;
	user_device.bmAttributes = 0x02 ; //03 中断 02 批量
	user_device.bInEndpointAddress = 0x81;
	user_device.bInterfaceNumber = 0;
	user_device.dev = NULL;
	FILE *fp;
 	char filename[40];
	char localtime[40];
	//time
	time_t tm_ptr;
	struct tm *ptr = NULL;
	time(&tm_ptr);
	ptr = gmtime(&tm_ptr);
	sprintf(filename,"gps_%d_%d_%d_%d_%d_%d.bag",(1900+ptr->tm_year),(1+ptr->tm_mon),ptr->tm_mday,ptr->tm_hour+8,ptr->tm_min,ptr->tm_sec);
	
	
	
 	while(1){
 		//初始化函数库
	 	init_libusb();
	 	//获取设备描述符
		rv = get_device_descriptor(&dev_desc,&user_device);
		if(rv < 0) {
				
			perror("\nget_device_descriptor error");
		}
	 	
		
		
		//打开设备
		g_usb_handle = libusb_open_device_with_vid_pid(NULL, user_device.idVendor, user_device.idProduct);
		if(g_usb_handle == NULL) {
			
			perror("\nopen_device error");
			sleep(1);
			
		}
	 
		rv = libusb_claim_interface(g_usb_handle,user_device.bInterfaceNumber);
		if(rv < 0) {
			rv = libusb_detach_kernel_driver(g_usb_handle,user_device.bInterfaceNumber);
			if(rv < 0) {
				
				perror("\ndetach_kernel_driver error"); 
				
			}
			rv = libusb_claim_interface(g_usb_handle,user_device.bInterfaceNumber);
			if(rv < 0)
			{
				
				perror("\nlibusb_claim_interface error");
				
			}
	 
		}
	 
	 	memset(buf,0,sizeof(buf));
		rv = libusb_bulk_transfer(g_usb_handle,user_device.bInEndpointAddress,buf,sizeof(buf),&length,1000);	
		if(rv < 0) {
		
			perror("\nbulk_transfer error");
			
		}
	 	else{
	 		
	 	/*	fp=fopen(filename,"ab+");
			//update time
			time(&tm_ptr);
			ptr = gmtime(&tm_ptr);
			clock_t t = clock();
			int ms = 0;
			ms = t * 1000 / CLOCKS_PER_SEC % 1000;
			sprintf(localtime,"\n\ntime %d%02d%02d%02d%02d%02d%03d\n",(1900+ptr->tm_year),(1+ptr->tm_mon),ptr->tm_mday,ptr->tm_hour+8,ptr->tm_min,ptr->tm_sec,ms);
			fputs(localtime,fp);
	 		fputs(buf,fp);
			
	 		fclose(fp);
	 		printf("receive:%s size:%d\n",buf,sizeof(buf));
	*/	/*
			u8 *addr1 = strstr(buf,"$GPRMC");
			u8 *addr2 = strstr(buf,"$GPGSV");

			
			int len = addr2-addr1;
			printf("%d\n",len);
			u8 *temp_buf = (u8 *)malloc(len*sizeof(u8));
			
			memcpy(temp_buf,buf,sizeof(u8)*len);
			printf("%s\n",temp_buf);
	 		free(temp_buf);
		*/
		printf("buf %s\n",buf);
	 	}
			 	
		libusb_close(g_usb_handle);
	 
		libusb_release_interface(g_usb_handle,user_device.bInterfaceNumber);
	 
		libusb_free_device_list(user_device.devs, 1);
	 
		libusb_exit(NULL);
	
	}
 	
	return 0;
}

